import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:wallet_utils/color.dart';
import 'package:wallet_utils/login_utils.dart';
import 'package:wallet_utils/report_widget/click_widget.dart';
import 'package:wallet_utils/text_style.dart';
import 'package:get/get.dart';

///title：用户协议弹窗

class AgreementDialog extends Dialog {
  ScrollController _scrollController = ScrollController();
  final void Function() onTap;
  String? name='';
  String? content;
  String? submit = '';
  bool? isClose = false;
  bool? isCloseBt=false;

  AgreementDialog({required this.onTap, this.name, this.content, this.submit, this.isClose = false,this.isCloseBt=false});

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(maxWidth: MediaQuery.of(context).size.width, maxHeight: MediaQuery.of(context).size.height),
      context: context,
      designSize: const Size(375, 812),
      orientation: Orientation.portrait,
    );
    return WillPopScope(
      child: Material(
        type: MaterialType.transparency,
        child: Center(
          child: Container(
            width: double.infinity,
            height: 500.w,
            margin: EdgeInsets.only(left: 20.w, right: 20.w),
            alignment: Alignment.center,
            decoration: BoxDecoration(color: ColorManager.colorFFFFFF, borderRadius: BorderRadius.circular(4.w)),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(height: 15.w),
                InkWell(
                  onTap: () async {
                    if (isClose!) {
                      Get.back();
                    } else {
                      await LoginUtils.saveAgreementBool(true);
                      if (Platform.isIOS) {
                        exit(0);
                      } else if (Platform.isAndroid) {
                        SystemNavigator.pop();
                      }
                    }
                  },
                  child: Visibility(
                    visible: isClose!,
                    child: Container(
                      margin: EdgeInsets.only(right: 16.w),
                      alignment: Alignment.centerRight,
                      child: Image.asset(
                        'images/icon_close.png',
                        width: 10.w,
                        height: 10.w,
                      ),
                    ),
                  ),
                ),
                Visibility(
                  visible: name!.isNotEmpty,
                  child: Text(name ?? '',style: TextStyleUtils.style2A2A2A(fontSize:16.sp,fontWeight: FontWeight.bold ),),
                ),
                SizedBox(height: 15.w),
                Expanded(
                  child: MediaQuery.removePadding(
                    removeTop: true,
                    context: context,
                    child: ListView(
                      controller: _scrollController,
                      shrinkWrap: true,
                      children: [
                        Container(
                          margin: EdgeInsets.only(left: 16.w, right: 16.w),
                          child: HtmlWidget(
                            content ?? '',
                            buildAsync: false,
                            textStyle: TextStyleUtils.style000000(fontSize: 13.sp),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),

                InkWell(
                  onTap: onTap,
                  //     (){
                  //   LoginUtils.saveAgreementBool(false);
                  //   Navigator.pop(context);
                  // },
                  child: Container(
                    width: double.infinity,
                    height: 40.w,
                    margin: EdgeInsets.only(top: 20.w, bottom: 20.w, left: 30.w, right: 30.w),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: ColorManager.colorD93C3A,
                      borderRadius: BorderRadius.circular(20.w),
                    ),
                    child: Text(
                      submit ?? '同意',
                      style: TextStyle(
                        fontSize: 15.sp,
                        color: ColorManager.colorFFFFFF,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                ),
                Visibility(
                  visible: isCloseBt!,
                  child: ClickWidget(
                    onTap: (){
                      if (Platform.isIOS) {
                        exit(0);
                      } else if (Platform.isAndroid) {
                        SystemNavigator.pop();
                      }
                    },
                    child: Container(
                      width: double.infinity,
                      height: 40.w,
                      margin: EdgeInsets.only(bottom: 20.w, left: 30.w, right: 30.w),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: ColorManager.colorC2C2C2,
                        borderRadius: BorderRadius.circular(20.w),
                      ),
                      child: Text(
                        '拒绝',
                        style: TextStyle(
                          fontSize: 15.sp,
                          color: ColorManager.colorFFFFFF,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  ),
                ),
                // SizedBox(height: 30.w),
              ],
            ),
          ),
        ),
      ),
      onWillPop: () async {
        return false;
      },
    );
  }
}
